#!/usr/bin/python
# coding=utf-8

# List of members of parliament
import urllib
import urlparse
import re
#from openpolitics.feedreader.readers import senatgovpl
from helpers import read_url, decode, split_name

patterns = {
    'person': re.compile('<li><a HREF="([^"]+)">([^<]+)</A>', re.I|re.M),
}


def get_members(path):
    source = read_url(path)
    members = []

    matches = patterns['person'].findall(source)
    for match in matches:
        link = urlparse.urljoin(path, match[0])
        name = split_name(match[1].strip())
        members.append({
            'url': link,
            'first_name': decode(name['first']),
            'middle_name': decode(name['middle']),
            'last_name': decode(name['last']),
        })
    return members

def main():
    path = 'http://www.senat.gov.pl/k7/senat/senator.htm'
    members = get_members(path)
    for member in members:
        print member
    
    
    

if __name__ == "__main__":
    main()
